package com.beordie.controller;

import com.beordie.model.Car;
import com.beordie.model.FormatCar;
import com.beordie.model.Message;
import com.beordie.model.ResultData;
import com.beordie.mvc.ResponseText;
import com.beordie.service.ICarService;
import com.beordie.service.impl.CarServiceImpl;
import com.beordie.utils.DateFormatUtils;
import com.beordie.utils.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Classname CarController
 * @Description
 * @Date 2021/7/11 21:24
 * @Created 30500
 */
public class CarController {
    ICarService carService = new CarServiceImpl();

    @ResponseText("/car/getAll.udo")
    public String getAllCars(HttpServletRequest request, HttpServletResponse response) {
        //1.    获取查询数据的起始索引值
        String offsetS = request.getParameter("offset") == null ? "0" : request.getParameter("offset");
        int offset = Integer.parseInt(offsetS);

        //2.    获取当前页要查询的数据量
        String pageNumberS = request.getParameter("pageNumber") == null ? "5" : request.getParameter("pageNumber");
        int pageNumber = Integer.parseInt(pageNumberS);

        List<FormatCar> result = carService.getCarsByLimit(true, offset, pageNumber);
        int total = carService.getCount();

        //4.    将集合封装为 bootstrap-table识别的格式
        ResultData<FormatCar> resultData = new ResultData<>();
        resultData.setRows(result);
        resultData.setTotal(total);
        return JsonUtils.parseObject(resultData);
    }

    @ResponseText("/car/insert.udo")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String model = request.getParameter("model");
        String factory = request.getParameter("factory");
        Date createTime = DateFormatUtils.getTimeDate(request.getParameter("time"));
        int seat = Integer.parseInt(request.getParameter("seat"));
        String color = request.getParameter("color");
        int price = Integer.parseInt(request.getParameter("price"));

        Car car = new Car(0, number, model, factory, createTime, seat, color, price);

        int result = carService.insert(car);
        System.out.println(result);
        Message message = new Message();
        if (result > 0) {
            message.setStatus(0);
            message.setResult("新增成功");
        } else {
            message.setStatus(-1);
            message.setResult("新增错误");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/car/findByNumber.udo")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        FormatCar car = carService.getCarByNumber(number);

        Message message = new Message();
        if (car != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(car);
        } else {
            message.setStatus(-1);
            message.setResult("查询失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/car/delete.udo")
    public String deleteById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        Message message = new Message();
        int result = carService.deleteById(id);
        if (result > 0) {
            message.setStatus(0);
            message.setResult("删除成功");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/car/update.udo")
    public String updateById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        String number = request.getParameter("number");
        String model = request.getParameter("model");
        String factory = request.getParameter("factory");
        Date createTime = DateFormatUtils.getTimeDate(request.getParameter("time"));
        int seat = Integer.parseInt(request.getParameter("seat"));
        String color = request.getParameter("color");
        int price = Integer.parseInt(request.getParameter("price"));

        Car car = new Car(id, number, model, factory, createTime, seat, color, price);
        Message message = new Message();
        int result = carService.updateById(car);
        if (result > 0) {
            message.setStatus(0);
            message.setResult("更新成功");
        } else {
            message.setStatus(-1);
            message.setResult("更新失败");
        }
        return JsonUtils.parseObject(message);
    }
}
